<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item
              label="姓名"
              :labelCol="{span: 7,offset: 1}"
              :wrapperCol="{span: 15, offset: 1}"
            >
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item
                label="职称"
                :labelCol="{span: 7,offset: 1}"
                :wrapperCol="{span: 15, offset: 1}"
            >
              <a-input v-model="queryParam.zc"/>
            </a-form-item>
          </a-col>
          
          <!-- <a-col :md="8" :sm="12">
              <a-form-item label="培养单位">
                <a-select allowClear v-model="queryParam.pydw" @change="yxChange" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="专业">
                <a-select allowClear v-model="queryParam.zyh" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.zyList" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
          </a-col> -->
          <a-col :md="12" :sm="12">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
              <a-button type="primary" @click="showCheck" style="margin:0 10px">新增</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <div style="margin-top: 10px; background-color: white">
      <s-table
        v-if="init"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :scroll="{ x: 1000 }"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
        :rowSelection="options.rowSelection"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <!-- <a slot="xh" slot-scope="a,b" @click="direct(b)">{{a}}</a> -->
        <template slot="operation" slot-scope="id, record">
          <a @click="showCheck(record)">修改</a>
          <a @click="deleteList(record.id)">删除</a>
        </template>
      </s-table>
    </div>

    <a-modal
      title="评审专家库"
      :visible="visible"
      :bodyStyle="{
        height: '400px',
        overflow: 'auto'
      }"
      okText="提交"
      @ok="sava()"
      @cancel="handleCancel()"
    >
      <a-form :form="form">
          <a-col :span="24">
            <a-form-item
                label="职称"
                :labelCol="{span: 6,offset: 1}"
                :wrapperCol="{span: 16}"
            >
              <a-input v-decorator="['zc',
          {
            rules: [{ required: true, message: '请输入职称' }],
          }]"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item
              label="姓名"
              :labelCol="{span: 6,offset: 1}"
                :wrapperCol="{span: 16}"
            >
              <a-input v-decorator="['xm',
          {
            rules: [{ required: true, message: '请输入姓名' }],
          }]"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="所属院系" :labelCol="{span: 6,offset: 1}" :wrapperCol="{span: 16}">
                <a-select allowClear v-decorator="['ssyxm',{ rules: [{ required: true, message: '请选择培养单位' }],}]" @change="yxChange" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item
              label="所在单位"
              :labelCol="{span: 6,offset: 1}"
                :wrapperCol="{span: 16}"
            >
              <a-input v-decorator="['szdw',
          {
            rules: [{ required: true, message: '请输入所在单位' }],
          }]"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item
              label="现从事专业"
              :labelCol="{span: 6,offset: 1}"
                :wrapperCol="{span: 16}"
            >
              <a-input v-decorator="['xcszy',
          {
            rules: [{ required: true, message: '请输入现从事专业' }],
          }]"/>
            </a-form-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-item label="所在单位" :labelCol="{span: 6,offset: 1}" :wrapperCol="{span: 16}">
                <a-select allowClear v-decorator="['ssyxm',{ rules: [{ required: true, message: '请选择培养单位' }],}]" @change="yxChange" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="专业" :labelCol="{span: 6,offset: 1}" :wrapperCol="{span: 16}">
                <a-select allowClear v-decorator="['xcszy', {rules: [{ required: true, message: '请输入专业' }], }]" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.zyList" :key="i.id" :value="i.zymc">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
            </a-form-item>
          </a-col> -->

      </a-form>
    </a-modal>
  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import searchMixin from '@/utils/searchRemain'

export default {
  mixins: [ searchMixin ],
  data () {
    return {
      form: this.$form.createForm(this),
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '序号',
          width:69,
          fixed: 'left',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '姓名',
          width: 80,
          dataIndex: 'xm'
        },
        {
          title: '职称',
          dataIndex: 'zc',
          width:120,
        },
        {
          title: '所在单位',
          dataIndex: 'szdw'
        },
        {
          title: '现从事专业',
          dataIndex: 'xcszy'
        },
        {
          title: '操作',
          dataIndex: 'id',
          width: 120,
          scopedSlots: { customRender: 'operation' },
          fixed: 'right'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.zjkList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {},
      visible: false,
      init: false,
      editInfo: {},
      fileList: [],
      selectedRowKeys: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
    }
  },
  components: {STable},
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0075: state => state.app['TYPE0075'],
      TYPE0126: state => state.app['TYPE0126'],
      TYPE0503: state => state.app['TYPE0503'],
      TYPE0337: state => state.app['TYPE0337'],
    }),
    routeName () {
      return this.$route.name
    }
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0503')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0337')
  },
  watch: {
    'routeName' (name) {
      this.queryParam = {}
      this.$set(this.queryParam, 'sznj', this.systemInfo.currentGrade)
      this.$refs.table.refresh(true)
    }
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data
        }
        //this.$set(this.queryParam, 'sznj', res[1].currentGrade)
        this.init = true
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    showCheck (info) {
      this.visible = true
      if(info){
        this.editInfo = info
        this.$nextTick(()=>{
        let keys = Object.keys(this.form.getFieldsValue())
        let obj ={}
        keys.forEach(item =>{
          obj[item] = this.editInfo[item]
        })
        this.form.setFieldsValue(obj)
      })
      }
    },
    handleCancel(){
      this.visible = false
      this.editInfo = {}
    },
    sava(){
      this.form.validateFields((err, values) => {
        if (err) return
        let params = {}
        // this.selectMenu.yxList.map((item)=>{
        //   if(item.dwh == values.ssyxm){
        //     values.szdw = item.dwmc
        //   }
        // })
         params = Object.assign({
          id : this.editInfo.id ? this.editInfo.id : ""
        }, values)
        this.$api.cultivate.savezjkList(params).then(res => {
          this.$message.success('提交成功')
          this.form.resetFields()
          this.visible = false
          this.editInfo = {}
          this.$refs.table.refresh(true)
        })
      })
      
    },
    // yxChange (code) {
    //   this.$set(this.queryParam, 'zyh', '')
    //   if (code) {
    //     this.$api.base.majorAll({lsdwh: code}).then(res => {
    //       this.$set(this.selectMenu, 'zyList', res.data)
    //     })
    //   } else {
    //     this.$set(this.selectMenu, 'zyList', [])
    //   }
    // },
    deleteList(id){
      this.$api.cultivate.zjkListDL({ids:[id]}).then(res => {
          this.$message.success('删除成功')
          this.$refs.table.refresh(true)
        })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
  }
}
</script>

<style>

</style>
